<?php
$loader->requireOnce('includes/ServiceDiscount/TicketItemPayment.class.php');

class CompositeServiceItemPayment extends TicketItemPayment {
	
	function __construct($discountBehaviour) {
		parent::__construct($discountBehaviour);
	}
	
	/**
	 * Calculate prices for action "update price of a ticket item"
	 *
	 * @param int $ticketItemId
	 * @param String $insuranceType
	 */
	public function add($ticketItemId, $insuranceType) {
		$compositeItem = Celini::newORDO('CompositeServiceItem');
		$res = $compositeItem->getAllServices($ticketItemId);
		$ticketItem = Celini::newORDO('TicketItem', $ticketItemId);
		//$compositeAsso = Celini::newORDO('CompositeServiceAsso');
		//$default = $compositeAsso->getDefaultServiceByCompositeService($ticketItem->get('item_id'));
		//$res = array_merge($default, $option);
		$result = array('original_price' => 0,
						'patient_price' => 0,
						'insurance_price' => 0,
						'discount' => 0);
		foreach ($res as $item) {			
			$price = $this->getServicePrice($item['service_id'], $insuranceType);
			
			// For test, dont discount on examination type
			// Only discount of sale program
			$temp = $this->calculate($item['service_id'], $price);
			$result['original_price'] += $price['original_price'];
			$result['patient_price'] += $temp['patient_price'];
			$result['insurance_price'] += $price['insurance_price'];
			$result['discount'] += $temp['discount'];
			
			$temp['original_price'] = $price['original_price'];
			$temp['insurance_price'] = $price['insurance_price'];
			
			$compositeItem->updateItemPrice($ticketItemId, $item['service_id'], $temp);		
		}
		$this->result = $result;		
	}
	private function calculate($serviceId, $price) {
		// discount on saleProgram
		$saleDiscount = $this->discountBehaviour->discountOnSaleProgram($serviceId, $price['patient_price']);
		// update discount 
		$price['discount'] = $saleDiscount;		
		return $price;		
	}
	
	public function update($ticketItemId) {
		// TODO: code
	}
}
?>